// xc_lshop/savemoney/stroll/index.js
var smapidata = require('../model/smapi_data.js')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    searchblock:false,
    sort:"1",
    keyword:"",
    modellist:[],
    searchmodellist:[],
    showall:false,
  },
  xcloaddata: function (options, config) {
    if(options.source){
      this.setData({
        opsource:options.source
      })
    }
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000'
    })
    var that=this;
    var update = {};
    if (config && config.jtklmapi ) {
      if(config.jtklmapi.status==1){
        update['strollconfig'] = config.jtklmapi.stroll

        var windowWidth="";
      
        wx.getSystemInfo({
          success: (result) => {
            windowWidth=result['windowWidth']
          },
        })
        var width=(windowWidth - 20) * 0.485 
        width=width.toFixed(2)
        this.setData({
          itemwidth:width
        })
        this.setData(update)
          
        this.getnavdata()
      }else{
        this.error("功能已关闭",true)
      }
    }else{
      this.error("省钱未配制",true)
    }
    
  },
  onsearchclick(e){
    wx.navigateTo({
      url: './search',
    })

  },
  
  changesource(e){
    console.log(e)
    var dataset = e.currentTarget.dataset;
    this.setData({
      source:dataset.source,
      showall:!this.data.showall
    })

    this.getclass(dataset.source)

  },
  clicksource(){
     this.setData({
      showall:!this.data.showall
     })
     
  },
  getnavdata(){
    var that=this;
    smapidata.getnavdata().then(function (ret){
    
      if(ret['status']=1){
        var update={
          platform:ret['data']
        }
        // ret['data'].splice(0,1)
        // if(ret['data'][0]){
        //   update['source']=ret['data'][0]['source']
        // }
        var opsource='pdd'
        if(that.data.opsource){
          opsource=that.data.opsource
        }
        update['source']=opsource
        
        that.setData(update)
        that.getclass(that.data.source)
        that.gethotlist(that.data.source,20)
        that.getpagelist(that,1)

      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }
    })
    // that.getpagelist(that,1)

  },
  gethotlist(source,num){
    var that=this;
    smapidata.gethotlist(source,num).then(function (ret){
      console.log("ret2222222222222")
      console.log(ret)
      if(ret['status']=1){
        var update={
          hotmodellist:ret['data']
        }
        that.setData(update)
      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }

    })
  },
  onSearch(e){
    console.log(e)
     this.setData({
      searchblock:true,
      keyword:e.detail
     })
     this.getsearchpagelist(this,1)
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere={};
        strwhere['source']=this.data.source
        strwhere['type']=2
      }
    }
    smapidata.hotoffset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
         if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"]['data'];
        } else {
          if (result["obj"]["modellist"]['data'].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]['data']);
          }
        }
      }     
      wx.stopPullDownRefresh();
   
      that.setData(updatemdoel);
    }, function () {
  
    });
  },
  getpropagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere={};
        strwhere['source']=this.data.source
        strwhere['classid']=this.data.classid
      }
      that.setData({
        promodellist: [],
      });
      // that.setData({
      //   skeleton: true
      // });
      // setTimeout(() => (
      //   that.setData({
      //     skeleton: false
      //   })
      // ), 600)
    }
    smapidata.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        // if (result["obj"]["modellist"].length < 1) {
        //   updatemdoel["isempty"] = true
        // } else
         if (rest == 1) {
          updatemdoel["promodellist"] = result["obj"]["modellist"]['data'];
        } else {
          if (result["obj"]["modellist"]['data'].length > 0) {
            updatemdoel["promodellist"] = that.data.promodellist.concat(result["obj"]["modellist"]['data']);
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      wx.stopPullDownRefresh();
   
      that.setData(updatemdoel);
    }, function () {
  
    });
  },
  getsearchpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere={};
        strwhere['keyword']=this.data.keyword
        strwhere['source']=this.data.source
        strwhere['sort']=this.data.sort
      }
    }
    smapidata.searchoffset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      console.log("this.data.searchmodellist")
      console.log(result)
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
         if (rest == 1) {
          updatemdoel["searchmodellist"] = result["obj"]["modellist"]['data'];
        } else {
          if (result["obj"]["modellist"]['data'].length > 0) {
            updatemdoel["searchmodellist"] = that.data.searchmodellist.concat(result["obj"]["modellist"]['data']);
          }
        }
      }     
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
      console.log("this.data.searchmodellist")
      console.log(that.data.searchmodellist)
    }, function () {
  
    });
  },
  getclass(source){
    var that=this;
    smapidata.getclass(source).then(function (ret){
      console.log("getclass")
      console.log(ret)
      if(ret['status']=1){
        var classs=ret['data']
        var update={}
        console.log(ret['data'])
        if(source=='douyin'){
          update['classid']='1'

        }else{
          classs[0]="全部"
          update['classid']=0
        }
        update['class']=classs

        console.log("update")
        console.log(update)

        that.setData(update)

        if(update['classid']==0){
          that.gethotlist(that.data.source,20)
          that.getpagelist(that,1)
        }else{
          that.getpropagelist(that, 1)
        }
        
      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }

    })
  },
  onChange(e){
    var  that=this;
    this.setData({
      classid:e.detail.name
    })
    if(e.detail.name!=0){
      that.getpropagelist(that, 1)
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    if(this.data.classid==0){
      that.getpagelist(that,-1)
    }else{
      that.getpropagelist(that,-1)

    }

  },


})